/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.PhoneBookDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class PhoneBookDaoImpl extends BaseDaoImpl implements PhoneBookDao {
	private String getBlearSearchHql(String searchKey) {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((p.perName like :searchKey)  or (p.pnSpell like :searchKey) or (p.mobile like :searchKey))";
		}

		return str;
	}

	private void setBlearSearchValue(Query q, String searchKey) {
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			q.setString("searchKey", "%" + searchKey + "%");
		}

	}

	public List getList(long stafferId, String searchKey, int startIndex,
			int num) {
		String str = this.getBlearSearchHql(searchKey);
		String sql = "from com.minierp.model.PhoneBook p  where p.stafferId="
				+ stafferId + str + " order by p.pnSpell,p.id";
		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, searchKey);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}

	public int getListCount(long stafferId, String searchKey) {
		String str = this.getBlearSearchHql(searchKey);
		String sql = "select count(p.id) from com.minierp.model.PhoneBook p  where p.stafferId="
				+ stafferId + str;
		Session sess = null;

		int arg9;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, searchKey);
			List list = q.list();
			if (list.isEmpty()) {
				return 0;
			}

			arg9 = Integer.parseInt(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg9;
	}

	public List getList(long stafferId) {
		String sql = "from com.minierp.model.PhoneBook p where p.stafferId="
				+ stafferId + " order by p.pnSpell,p.id";
		return this.getHibernateTemplate().find(sql);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.PhoneBook where id = :id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}
}